package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemServicePlus;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.bytebuddy.asm.Advice;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImplPlus extends ServiceImpl<ServiceItemMapper,ServiceItem> implements IServiceItemServicePlus {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private BpmnInfoServiceImplPlus bpmnInfoServiceImplPlus;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private CarPackageAuditServiceImplPlus carPackageAuditServiceImplPlus;
    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        IPage<ServiceItem> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<ServiceItem> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.hasText(qo.getName()),"name",qo.getName())
                .eq(qo.getCarPackage() != null,"car_package",qo.getCarPackage())
                .eq(qo.getServiceCatalog() != null,"service_catalog",qo.getServiceCatalog())
                .eq(qo.getAuditStatus() != null,"audit_status",qo.getAuditStatus())
                .eq(qo.getSaleStatus() != null,"sale_status",qo.getSaleStatus());
        return new TablePageInfo<ServiceItem>(super.page(page,wrapper));
    }

    @Override
    public void saleOn(Long id) {
        /*//数据处于上架状态,再点击上架，抛出异常.
        if(ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("服务单项已经处于上架状态");
        }
        //如果是套餐,并且不是审核通过的状态，是不能上架的，抛出异常.
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) &&
                !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("套餐审核通过才可以进行上架");
        }
        //非套餐，可以直接进行上架
        //通过id把状态修改为上架
        serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_ON);*/

        ServiceItem serviceItem = serviceItemMapper.selectById(id);
        if(ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())){
            throw new BusinessException("服务单项已经处于上架状态");
        }
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage()) &&
                !ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("套餐审核通过才可以进行上架");
        }
        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id)
                .set("sale_status",ServiceItem.SALESTATUS_ON);
        serviceItemMapper.update(serviceItem,wrapper);
    }

    @Override
    public void saleOff(Long id) {
        //通过id把状态修改为下架
        /*serviceItemMapper.changeSaleStatus(id,ServiceItem.SALESTATUS_OFF);*/

        UpdateWrapper<ServiceItem> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id)
                .set("sale_status",ServiceItem.SALESTATUS_OFF);
        serviceItemMapper.update(null,wrapper);
    }

    @Override
    public void startAudit(Long serviceItemId, Long bpmnInfoId, Long director, Long finance, String info) {
        //通过服务单项明细获取单项明细对象,进行合理性校验
        //(只有处于下架状态，是套餐且审核状态为初始化或审核拒绝才可发起审核)
        //创建CarPackageAudit对象
        //启动流程实例,绑定业务标识,并把map也一并传入
        //1.1获取流程定义的key
        //将服务单项明细的套餐状态更改为审核中
        ServiceItem serviceItem = serviceItemMapper.selectById(serviceItemId);
        if(ServiceItem.SALESTATUS_ON.equals(serviceItem.getSaleStatus())
                || ServiceItem.CARPACKAGE_NO.equals(serviceItem.getCarPackage())
                || ServiceItem.AUDITSTATUS_AUDITING.equals(serviceItem.getAuditStatus())
                || ServiceItem.AUDITSTATUS_APPROVED.equals(serviceItem.getAuditStatus())
                || ServiceItem.AUDITSTATUS_NO_REQUIRED.equals(serviceItem.getAuditStatus())){
            throw new BusinessException("参数异常");
        }
        CarPackageAudit audit = new CarPackageAudit();
        audit.setServiceItemId(serviceItemId);
        audit.setServiceItemInfo(serviceItem.getInfo());
        audit.setServiceItemPrice(serviceItem.getDiscountPrice());
        audit.setCreator(ShiroUtils.getUser().getUserName());
        audit.setBpmnInfoId(bpmnInfoId);
        audit.setInfo(info);
        audit.setAuditorId(director);
        audit.setCreateTime(new Date());
        audit.setServiceItemName(serviceItem.getName());
        BpmnInfo bpmnInfo = bpmnInfoServiceImplPlus.getById(bpmnInfoId);
        Map<String,Object> map = new HashMap<>();
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        if(director != null){
            map.put("director",director);
        }
        if(finance != null){
            map.put("finance",finance);
        }
        carPackageAuditServiceImplPlus.save(audit);
        String processKey = bpmnInfo.getActProcessKey();
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processKey, String.valueOf(audit.getId()), map);
       //audit.setInstanceId(processInstance.getId());

        UpdateWrapper<CarPackageAudit> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",audit.getId())
                .set("instance_id",processInstance.getId());
        carPackageAuditServiceImplPlus.update(null,wrapper);

       // serviceItemMapper.changeAuditStatus(serviceItemId,ServiceItem.AUDITSTATUS_AUDITING);
        UpdateWrapper<ServiceItem> wrapper1 = new UpdateWrapper<>();
        wrapper1.eq("id", serviceItemId)
                .set("audit_status",ServiceItem.AUDITSTATUS_AUDITING);
        serviceItemMapper.update(null,wrapper1);
    }


    /*@Override
    public void changeAuditStatus(Long serviceItemId, Integer status) {
        serviceItemMapper.changeAuditStatus(serviceItemId,status);
    }*/


    @Override
    public BpmnInfo queryByBpmnType(String car_package) {
        QueryWrapper<BpmnInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bpmn_type", car_package);
        return bpmnInfoServiceImplPlus.getOne(wrapper);
    }
}
